package com.ruoyi.back.service.v3.impl

import cn.hutool.core.bean.BeanUtil
import cn.hutool.core.util.StrUtil
import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.baomidou.mybatisplus.extension.plugins.pagination.Page
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
import com.ruoyi.app.common.*
import com.ruoyi.back.domain.v3.CheckStatusEnum
import com.ruoyi.back.domain.v3.MyRecord
import com.ruoyi.back.domain.v3.dto.UserRecordSearchDto
import com.ruoyi.back.domain.v3.vo.UserRecordListVo
import com.ruoyi.back.domain.v3.vo.UserRecordNum
import com.ruoyi.back.mapper.MyRecordMapper
import com.ruoyi.back.service.v3.MyRecordService
import com.ruoyi.common.core.page.TableSupport
import org.springframework.stereotype.Service
import java.util.*

@Service
class MyRecordServiceImpl : MyRecordService, ServiceImpl<MyRecordMapper, MyRecord>() {

    override fun userRecordList(userRecordSearchDto: UserRecordSearchDto): Page<UserRecordListVo> =
        TableSupport.buildPageRequest().let { pageDomain ->

            page(
                Page(
                    pageDomain.pageNum.toLong(),
                    pageDomain.pageSize.toLong()
                ), KtQueryWrapper(MyRecord::class.java)
                    .eq(
                        StrUtil.isNotBlank(userRecordSearchDto.userInfo),
                        MyRecord::username,
                        userRecordSearchDto.userInfo
                    )
                    .or().eq(
                        StrUtil.isNotBlank(userRecordSearchDto.userInfo),
                        MyRecord::userInfoId,
                        userRecordSearchDto.userInfo
                    )
                    .like(
                        StrUtil.isNotBlank(userRecordSearchDto.prompt),
                        MyRecord::descText,
                        userRecordSearchDto.prompt
                    )
                    .eq(userRecordSearchDto.type == 2, MyRecord::recommendBanner, true)
                    .eq(userRecordSearchDto.type == 3, MyRecord::shelves, true)
                    .eq(userRecordSearchDto.type == 4, MyRecord::shelves, false)
                    .eq(MyRecord::recommendStatus, CheckStatusEnum.CHECK_PASS.type)
                    .gt(
                        StrUtil.isNotBlank(userRecordSearchDto.startUpTime),
                        MyRecord::shelvesTime,
                        userRecordSearchDto.startUpTime.date()
                    )
                    .lt(
                        StrUtil.isNotBlank(userRecordSearchDto.endUpTime),
                        MyRecord::shelvesTime,
                        userRecordSearchDto.endUpTime.date()
                    )
                    .orderByDesc(MyRecord::shelvesTime)
            )
                .let { myRecordPage ->
                    Page<UserRecordListVo>().apply {
                        BeanUtil.copyProperties(myRecordPage, this)
                        records = myRecordPage.records.takeIf { it.isNotEmpty() }
                            ?.run {
                                map {
                                    UserRecordListVo().apply {
                                        BeanUtil.copyProperties(it, this)
                                        pics = it.pics?.split(",")
                                        shelvesTime = it.shelvesTime?.format()
                                    }
                                }
                            }
                    }
                }
        }

    override fun adminAddUserRecord(adminRecord: MyRecordService.AdminRecord): Data<Boolean> =
        getById(adminRecord.id)?.let { myRecord ->
            Ok("修改成功", updateById(myRecord.apply {
                BeanUtil.copyProperties(adminRecord, this)
                recommend = true
                admin = true
                recommendStatus = CheckStatusEnum.CHECK_PASS.type
                shelvesTime = if (adminRecord.shelves!!) Date() else null
            }))
        } ?: run {
            Ok("添加成功", save(MyRecord().apply {
                BeanUtil.copyProperties(adminRecord, this)
                recommend = true
                admin = true
                recommendStatus = CheckStatusEnum.CHECK_PASS.type
                shelvesTime = if (adminRecord.shelves!!) Date() else null
            }))
        }

    override fun removeUserRecord(ids: String): Data<Boolean> {

        ids.split(",").forEach {
            getById(it)?.run {
                if (!shelves!!) removeById(it)
            }
        }
        return Ok("批量移除成功", true)
    }

    override fun userRecordNum(): Data<UserRecordNum> =
        Ok("获取成功", UserRecordNum().apply {
            recommendNum = count(
                KtQueryWrapper(MyRecord::class.java)
                    .eq(MyRecord::recommendBanner, true)
            )
            upper = count(
                KtQueryWrapper(MyRecord::class.java)
                    .eq(MyRecord::shelves, true)
            )
            downer = count(
                KtQueryWrapper(MyRecord::class.java)
                    .eq(MyRecord::recommendStatus, CheckStatusEnum.CHECK_PASS.type)
                    .eq(MyRecord::shelves, false)
            )
        })

    override fun recommendBanner(recordId: Long): Data<Boolean> =
        getById(recordId)?.run {
            when (recommendBanner) {
                true -> {
                    Ok("取消推荐到banner成功", updateById(this.apply { recommendBanner = false }))
                }

                else -> {
                    Ok("推荐到banner成功", updateById(this.apply { recommendBanner = true }))
                }
            }
        } ?: Fail("不存在资源")

    override fun upRecord(recordId: Long): Data<Boolean> =
        getById(recordId)?.run {
            when (shelves) {
                true -> {
                    Ok(
                        "下架成功",
                        updateById(this.apply { shelves = false; shelvesTime = null; recommendBanner = false })
                    )
                }

                else -> {
                    Ok("上架成功", updateById(this.apply { shelves = true; shelvesTime = Date(); }))
                }
            }
        } ?: Fail("不存在资源")

    override fun topRecord(recordId: Long): Data<Boolean> =
        getById(recordId)?.run {
            when (top) {
                true -> {
                    Ok("取消置顶成功", updateById(this.apply { top = false }))
                }

                else -> {
                    Ok("置顶成功", updateById(this.apply { top = true; recommendBanner = true }))
                }
            }
        } ?: Fail("不存在资源")


}